package com.demo.cms.webapp.template.directive;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;

import com.demo.cms.model.Article;
import com.demo.cms.model.Category;
import com.demo.cms.service.ArticleService;
import com.demo.cms.service.CategoryService;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.webapp.views.freemarker.directive.core.PageContextDirectiveSupport;

import freemarker.core.Environment;
import freemarker.core.Environment.TemplateDirectiveBodyEx;
import freemarker.template.SimpleHash;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

/**
 * 文章列表指令
 * 通过栏目ID和文章列表长度查询文章信息<br>
 * 文章列表指令中的文章将不包含文章内容信息。
 * @author ryuu.kk
 *
 */
public class ArticleListDirective extends PageContextDirectiveSupport {

	/**
	 * 页面模板名
	 */
	private static final String ARTICLE_LIST_TEMPLATE_NAME = "classpath:/com/demo/cms/webapp/template/articleList.ftl";
	
	/**
	 * 文章数据服务
	 */
	@Autowired
	private ArticleService articleService;
	
	/**
	 * 栏目数据服务
	 */
	@Autowired
	private CategoryService categoryService;

	@SuppressWarnings("rawtypes")
	@Override
	public void preWriter(Environment env, TemplateDirectiveBodyEx bodyEx,
			TemplateModel[] loopVars, SimpleHash hash, Map params)
			throws IOException, TemplateException {

		Long categoryId = null;
		// 栏目模型
		Category category = null;
		// 文章列表
		List<Article> articleList;
		//取得语言
		String i18n = "";
		Integer limit;
		Integer sort;
		// 包含栏目
		boolean hasCategory;
		String orderBy = "";
		categoryId = getLong("categoryId", params);
		
		// 国际化CODE
		i18n = getString("i18n", params);
		if (StringUtils.isBlank(i18n)) {
			i18n = LocaleContextHolder.getLocale().toString();
		}
		hasCategory = getBoolean("hasCategory", params, Boolean.valueOf(getDefaultValue("hasCategory")));
		// 栏目查询
		category = categoryService.find(categoryId);
		if (category == null) {
			throw new TemplateException("not found category id = " + categoryId, env);
		}

		// 取得检索大小
		limit = getInteger("limit", params, Integer.parseInt(getDefaultValue("limit")));
		// 取得排序类型
		sort = getInteger("sort", params, Integer.parseInt(getDefaultValue("sort")));

		switch (sort) {
		// 排序方式  1:置顶级别(默认); 2:最近发布; 3:评论最多
		case 1:
			orderBy = ArticleService.ORDER_BY_TOP_LEVEL;
			break;
		case 2:
			orderBy = ArticleService.ORDER_BY_NEW;
			break;
		case 3:
			orderBy = ArticleService.ORDER_BY_COMMENT;
			break;
		case 4 :
			orderBy = ArticleService.ORDER_BY_NEW_DESC;
			break;
		default:
			orderBy = ArticleService.ORDER_BY_TOP_LEVEL;
		}
		
		// 是否包含文章内容
		ResultSearchResult<Article> activeArticles = articleService.searchActive(categoryId, null, orderBy, limit);
		articleList = new ArrayList<Article>(activeArticles.getResults());
		// 栏目&文章列表
		if (hasCategory) {
			hash.put("category", category);
		}
		hash.put("channelPath", category.getChannelPath());
		hash.put("categoryPath", category.getPath());
		hash.put("articleList", articleList);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public String getTemplatePath(Map params) {
		String path = "";
		try {
			path = getString("path", params, ARTICLE_LIST_TEMPLATE_NAME);
		} catch (TemplateModelException e) {}
		return path;
	}
	
	@Override
	public void afterInvoke(Writer writer) {
		
	}
}
